package com.david4.jizhang.common;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

/**
 * 
 * @author <a href="mailto:hanxiaojun85@gmail.com">hanxiaojun</a>
 * @since 2010-5-10
 */
public class BaseModel {

	public void save() {
		PersistenceManager pm = null;
		try {
			pm = PMF.get().getPersistenceManager();
			pm.makePersistent(this);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<BaseModel> find() {
		List<BaseModel> results = null;
		PersistenceManager pm = null;
		Query query = null;
		try {
			pm = PMF.get().getPersistenceManager();
			query = pm.newQuery(this.getClass());
			results = (List) query.execute();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			query.closeAll();
			pm.close();
		}
		return results;
	}

	public void delete(Long id) {
		PersistenceManager pm = null;
		try {
			pm = PMF.get().getPersistenceManager();
			pm.deletePersistent(pm.getObjectById(this.getClass(), id));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pm.close();
		}
	}
}
